package freespiritsocial.io.user.weight

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.noober.background.drawable.DrawableCreator
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px
import freespiritsocial.io.user.R
import me.hgj.jetpackmvvm.base.fragment.BaseBottomSheetDialogFragment

/**
 * 列表底部弹窗
 */
class BottomCancelDialog(
    val mContext: Context,
    val title: String = "",
    val titleColor: Int = R.color.fff95862,
    val message: String = "",
    val confirmColor: Int = R.color.fff95862,
    val stroke: Int = 0,
    val confirmText: String = "Confirm",
    val callback: () -> Unit
): BaseBottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_bottom_cancel,container,false)

    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)
        init(view)
    }

    private fun init(view: View) = with(view) {
        findViewById<TextView>(R.id.tv_title)?.let {
            it.isVisible = title.isNotEmpty()
            it.setTextColor(context.getColor(titleColor))
            it.text = title
        }
        findViewById<TextView>(R.id.tv_message)?.let {
            it.isVisible = message.isNotEmpty()
            it.text = message
        }
        findViewById<TextView>(R.id.tv_cancel)?.setOnClickListener {
            dismiss()
        }
        findViewById<TextView>(R.id.tv_confirm)?.let {
            if (stroke > 0) {
                val bg =
                    DrawableCreator.Builder().setCornersRadius(dp2px(24f).toFloat()).setStrokeColor(context.getColor(confirmColor))
                        .setStrokeWidth(dp2px(stroke.toFloat()).toFloat())
                        .build()
                it.setTextColor(context.getColor(confirmColor))
                it.background = bg
            }
            it.text = confirmText
            it.setOnClickListener {
                callback()
                dismiss()
            }
        }
    }

}